﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class ResetPassword : System.Web.UI.Page
{
    RegistrationData regdata = new RegistrationData();
    string salt = string.Empty, Password = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {

            if (!(String.IsNullOrEmpty(txtnewPass.Text.Trim())))
            {
                txtnewPass.Attributes["value"] = txtnewPass.Text;
            }
            if (!(String.IsNullOrEmpty(txtnewPass.Text.Trim())))
            {
                txtnewPass.Attributes["value"] = txtnewPass.Text;
            }

        }
        if (HttpContext.Current.Request.QueryString["id"] != null)
        {
            DataSet ds = new DataSet();
            string id = string.Empty;
            string forgotpasswordlink = string.Empty;
            id = HttpContext.Current.Request.QueryString["id"].ToString();
            ds = regdata.SelectUserMaster_ForgotPasswordLink(id);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (id == ds.Tables[0].Rows[0]["ForgotPasswordLink"].ToString())
                    {
                        if (ds.Tables[0].Rows[0]["Email"] != null)
                        {
                            txtEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();
                            txtEmail.Enabled = false;
                            return;
                        }
                    }
                    else
                    {
                    }
                }
                else Response.Redirect(Common.LogOutPage);

            }
            else
            {
                //lblMessage.Text = "It seems that the requested URL has expired or is invalid.";
                //return;
            }
        }
        else Response.Redirect(Common.LogOutPage);
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (txtPassword.Text != string.Empty)
        {
            if (txtPassword.Text == txtnewPass.Text)
            {
                bool bflag = false;
                salt = regdata.RandomString(5);
                Password = regdata.protectpassword(regdata.protectpassword(txtPassword.Text) + salt);
                bflag = regdata.Update_ResetPassword(txtEmail.Text, Password,salt);
                if (bflag == true)
                {
                    lblError.Text = "Your Password has been changed <a href='" + EmailData.WebsiteLink + "' style='color:#00947b;'>click here </a> for login";
                }
                else
                {
                    lblError.Text = "Email ID does not exist";
                }
            }
            else
            {
                lblError.Text = "Password does not match";
            }
        }
        else lblError.Text = "Please enter a valid Password";
    }
     
}
